package com.zj.guava.eventbus;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;

public class AsyncEventBusExample {
    private static final Logger logger = LoggerFactory.getLogger(AsyncEventBusExample.class);

    public static void main(String[] args) {
        EventBus bus = new AsyncEventBus(Executors.newFixedThreadPool(3));
        bus.register(new SimpleListener());
        logger.info("bus start!");
        bus.post("simple event");
        bus.post("simple event222");
        logger.info("bus end!");
    }
    //main方法和test方法区别在与main中的线程池是非守护线程，main方法结束了线程也能继续运行。
    //test方法中线程池是守护线程，方法结束线程就结束了。

    @Test
    public void testSimpleEvent() throws InterruptedException {
        EventBus bus = new AsyncEventBus(Executors.newFixedThreadPool(2));
        bus.register(new SimpleListener());
        logger.info("bus start!");
        bus.post("simple event");
        bus.post("simple event222");
        logger.info("bus end!");
        Thread.sleep(10000);
    }
}
